package com.mvd.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.mvd.app.R;
import com.mvd.app.model.City;
import com.mvd.app.model.Group;
import com.mvd.app.model.MyFlagModel;

import java.util.List;


public class WaiJianResultAdapter2 extends BaseExpandableListAdapter {
    private Context ctx;
    private List<Group> groupList;
    LayoutInflater inflater;
    private MyClickListener mListener;

    public WaiJianResultAdapter2(Context ctx, List<Group> groupList, MyClickListener listener) {
        this.ctx = ctx;
        this.groupList = groupList;
        inflater = LayoutInflater.from(ctx);
        mListener = listener;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groupList.get(groupPosition).getCityList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition).getCityList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        final Group group = (Group) getGroup(groupPosition);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.group_item, null);
        }
        TextView groupText = (TextView) convertView.findViewById(R.id.groupText);
//        CheckBox groupCheckBox = (CheckBox) convertView.findViewById(R.id.groupCheckBox);
        groupText.setText(group.getName());
//        groupCheckBox.setChecked(group.isChecked());
//        groupCheckBox.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                group.changeChecked();
//                notifyDataSetChanged();
//                ((WaiJianResultActivity) ctx).reFlashGridView();
//            }
//        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        City city = (City) getChild(groupPosition, childPosition);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child_item2, null);
        }
        TextView childText = (TextView) convertView.findViewById(R.id.childText);
        CheckBox childCheckBox = (CheckBox) convertView.findViewById(R.id.childCheckBox);
        childText.setText(city.getName());
        childCheckBox.setChecked(city.isChecked());
        //0不适用
        TextView btn_bu_jiance = (TextView) convertView.findViewById(R.id.item_ac_waijian_btn_luntai_guige_r_b_preview);
        //1合格
        TextView btn_jiance = (TextView) convertView.findViewById(R.id.item_ac_waijian_btn_luntai_guige_r_b);
        //2 不合格
        TextView btn_wei_jiance = (TextView) convertView.findViewById(R.id.item_ac_waijian_btn_yulan_photo);

        int Selected = Integer.valueOf(city.getFlag());

        if (Selected == 1) {
            btn_bu_jiance.setBackground(ctx.getResources().getDrawable(R.drawable.red_003_40));
            btn_jiance.setBackground(ctx.getResources().getDrawable(R.drawable.i_hege));
            btn_wei_jiance.setBackground(ctx.getResources().getDrawable(R.drawable.red_003_40));
        } else if (Selected == 0) {
            btn_wei_jiance.setBackground(ctx.getResources().getDrawable(R.drawable.red_003_40));
            btn_jiance.setBackground(ctx.getResources().getDrawable(R.drawable.red_003_40));
            btn_bu_jiance.setBackground(ctx.getResources().getDrawable(R.drawable.i_no));
        } else if (Selected == 2) {
            btn_wei_jiance.setBackground(ctx.getResources().getDrawable(R.drawable.i_buhege));
            btn_jiance.setBackground(ctx.getResources().getDrawable(R.drawable.red_003_40));
            btn_bu_jiance.setBackground(ctx.getResources().getDrawable(R.drawable.red_003_40));
        }

        btn_bu_jiance.setOnClickListener(mListener);
        btn_bu_jiance.setTag(new MyFlagModel(groupPosition, childPosition, 0));
        btn_jiance.setOnClickListener(mListener);
        btn_jiance.setTag(new MyFlagModel(groupPosition, childPosition, 1));
        btn_wei_jiance.setOnClickListener(mListener);
        btn_wei_jiance.setTag(new MyFlagModel(groupPosition, childPosition, 2));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 用于回调的抽象类
     *
     * @author Ivan Xu
     *         2014-11-26
     */
    public static abstract class MyClickListener implements View.OnClickListener {
        /**
         * 基类的onClick方法
         */
        @Override
        public void onClick(View v) {
//            myOnClick((Integer) v.getTag(), v);
//            myOnClick(((MyFlagModel) v.getTag()).getPosition(), v, ((MyFlagModel) v.getTag()).getFlag());
            myOnClick(((MyFlagModel) v.getTag()).getGroupPosition(), ((MyFlagModel) v.getTag()).getChildPosition(), v, ((MyFlagModel) v.getTag()).getFlag());
        }

        //        public abstract void myOnClick(int position, View v, int flag);
        public abstract void myOnClick(int groupPosition, int childPosition, View v, int flag);
    }

}
